/**
 * ...
 * @author Thot
 */

function TransformGroup()
{
	this.matrix = new Matrix4x4();
	this.transformData = [1,0,0,1,0,0];
	this.subGroups = new Array();
}

TransformGroup.prototype.matrix 		= null;
TransformGroup.prototype.name 			= null;
TransformGroup.prototype.rotationX 		= 0;
TransformGroup.prototype.rotationY 		= 0;
TransformGroup.prototype.rotationZ 		= 0;
TransformGroup.prototype.scaleX			= 1;
TransformGroup.prototype.scaleY			= 1;
TransformGroup.prototype.scaleZ			= 1;
TransformGroup.prototype.pivotX			= 0;
TransformGroup.prototype.pivotY			= 0;
TransformGroup.prototype.pivotZ			= 0;
TransformGroup.prototype.x				= 0;
TransformGroup.prototype.y				= 0;
TransformGroup.prototype.z				= 0;
TransformGroup.prototype.parentGroup 	= null;
TransformGroup.prototype.transformData 	= null;	
TransformGroup.prototype.subGroups		= null;

TransformGroup.prototype.update = function()
{	
	
	this.x 		>>= 0;
	this.y 		>>= 0;
	this.z 		>>= 0;
	this.width 	>>= 0;
	this.height >>= 0;
	this.pivotX >>= 0;
	this.pivotY >>= 0;
	this.pivotZ >>= 0;
	this.rotationX = ( this.rotationX % 360 ) >> 0;
	this.rotationY = ( this.rotationY % 360 ) >> 0;
	this.rotationZ = ( this.rotationZ % 360 ) >> 0;
	this.matrix.identity();
	
	var i = 0;
	var max = this.subGroups.length;
	var group = null;
	
	if( this.parentGroup != null )
	{
		this.matrix.multiply(this.parentGroup.matrix);
	}
	

	this.matrix
		.translate( this.x + this.pivotX, this.y + this.pivotY, this.z + this.pivotZ )
		.scale( this.scaleX, this.scaleY, this.scaleZ )
		.rotateX( this.rotationX )
		.rotateY( this.rotationY )
		.rotateZ( this.rotationZ )
		.translate( -this.pivotX, -this.pivotY, -this.pivotZ );
	
	this.transformData = this.matrix.to2D();
	
	if( max > 0 )
	{
		for( i = 0; i < max; i++ )
		{
			group = this.subGroups[i];
			group.update();
		}
	}
	
};

TransformGroup.prototype.addSubGroup = function( p_group )
{
	p_group.parentGroup = this;
	this.subGroups.push( p_group );
};

TransformGroup.prototype.removeSubGroup = function( p_group )
{
	var index = this.subGroups.indexOf( p_group );
	if( index == -1 )
		return;
		
	this.subGroups[index].parentGroup = null;
	this.subGroups.splice( index, 1 );
};